local status, wk = pcall(require, 'which-key')
if not status then
  vim.notify('没有找到 which-key')
  return
end

-- vim.g.mapleader = ' '
-- vim.g.maplocalleader = ' '

wk.setup = {
  plugins = {
    marks = true, -- shows a list of your marks on ' and `
    registers = true, -- shows your registers on " in NORMAL or <C-r> in INSERT mode
    spelling = {
      enabled = false, -- enabling this will show WhichKey when pressing z= to select spelling suggestions
      suggestions = 20, -- how many suggestions should be shown in the list?
    },
    -- the presets plugin, adds help for a bunch of default keybindings in Neovim
    -- No actual key bindings are created
    presets = {
      operators = true, -- adds help for operators like d, y, ... and registers them for motion / text object completion
      motions = true, -- adds help for motions
      text_objects = true, -- help for text objects triggered after entering an operator
      windows = true, -- default bindings on <c-w>
      nav = true, -- misc bindings to work with windows
      z = true, -- bindings for folds, spelling and others prefixed with z
      g = true, -- bindings for prefixed with g
    },
  },
  -- add operators that will trigger motion and text object completion
  -- to enable all native operators, set the preset / operators plugin above
  operators = { gc = 'Comments' },
  key_labels = {
    -- override the label used to display some keys. It doesn't effect WK in any other way.
    -- For example:
    -- ['<space>'] = 'SPC',
    -- ["<cr>"] = "RET",
    -- ["<tab>"] = "TAB",
  },
  icons = {
    breadcrumb = '»', -- symbol used in the command line area that shows your active key combo
    separator = '➜', -- symbol used between a key and it's label
    group = '+', -- symbol prepended to a group
  },
  popup_mappings = {
    scroll_down = '<c-d>', -- binding to scroll down inside the popup
    scroll_up = '<c-u>', -- binding to scroll up inside the popup
  },
  window = {
    border = 'none', -- none, single, double, shadow
    position = 'bottom', -- bottom, top
    margin = { 1, 0, 1, 0 }, -- extra window margin [top, right, bottom, left]
    padding = { 2, 2, 2, 2 }, -- extra window padding [top, right, bottom, left]
    winblend = 0,
  },
  layout = {
    height = { min = 4, max = 25 }, -- min and max height of the columns
    width = { min = 20, max = 50 }, -- min and max width of the columns
    spacing = 3, -- spacing between columns
    align = 'left', -- align columns left, center or right
  },
  ignore_missing = false, -- enable this to hide mappings for which you didn't specify a label
  hidden = { '<silent>', '<cmd>', '<Cmd>', '<CR>', 'call', 'lua', '^:', '^ ' }, -- hide mapping boilerplate
  show_help = true, -- show help message on the command line when the popup is visible
  triggers = 'auto', -- automatically setup triggers
  -- triggers = {"<leader>"} -- or specify a list manually
  triggers_blacklist = {
    -- list of mode / prefixes that should never be hooked by WhichKey
    -- this is mostly relevant for key maps that start with a native binding
    -- most people should not need to change this
    i = { 'j', 'k' },
    v = { 'j', 'k' },
  },
}

local optsSpace = {
  mode = 'n', -- NORMAL mode
  prefix = '<Space>',
  buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings
  silent = true, -- use `silent` when creating keymaps
  noremap = true, -- use `noremap` when creating keymaps
  nowait = true, -- use `nowait` when creating keymaps
}

local mappingsSpace = {
  ['s,'] = { ':vertical resize -10<CR>', 'vertical resize -10' },
  ['s.'] = { ':vertical resize +10<CR>', 'vertical resize +10' },
  ['s='] = { '<C-w>=', 'the same ratio' },
  s = {
    name = 'split buff',
    v = { ':vsp<CR>', 'Split the screen vertically' },
    h = { ':sp<CR>', 'Split the screen horizontally' },
    c = { '<C-w>c', 'close current' },
    o = { '<C-w>o', 'close other' },
    j = { ':resize +10<CR>', 'horizontal resize +10' },
    k = { ':resize -10<CR>', 'horizontal resize -10' },
  },
  d = {
    --   map("n", "<leader>dd", ":call vimspector#Launch()<CR>", opt)
    -- map("n", "<leader>dq", ":call vimspector#Reset()<CR>", opt)
    -- map("n", "<leader>dr", ":call vimspector#Restart()<CR>", opt)
    -- map("n", "<leader>dc", ":call vimspector#Continue()<CR>", opt)
    -- map("n", "<leader>dt", ":call vimspector#ToggleBreakpoint()<CR>", opt)
    -- map("n", "<leader>dT", ":call vimspector#ClearBreakpoints()<CR>", opt)
    -- map("n", "<leader>dj", "<Plug>VimspectorStepOver", opt)
    -- map("n", "<leader>dk", "<Plug>VimspectorStepOut", opt)
    -- map("n", "<leader>dl", "<Plug>VimspectorStepInto", opt)
    -- map('n', '<leader>de', '<Plug>VimspectorBalloonEval', opt)
    name = 'debug',
    d = { ':call vimspector#Launch()<CR>', 'launch' },
    q = { ':call vimspector#Reset()<CR>', 'reset' },
    r = { ':call vimspector#Restart()<CR>', 'restart' },
    c = { ':call vimspector#Continue()<CR>', 'continue' },
    t = { ':call vimspector#ToggleBreakpoint()<CR>', 'breakpoint' },
    T = {
      '<Plug>VimspectorToggleConditionalBreakpoint',
      'condition breakpoint',
    },
    m = { ':call vimspector#ClearBreakpoints()<CR>', 'step over' },
    j = { '<Plug>VimspectorStepOver', 'setp over' },
    k = { '<Plug>VimspectorStepOut', 'step out' },
    l = { '<Plug>VimspectorStepInto', 'setp into' },
    e = { '<Plug>VimspectorBalloonEval', 'eval' },
  },
  b = {
    name = 'buff',
    c = { ':Bdelete!<CR>', 'close the current buff' },
    f = { ':Format<CR>', 'format' },
    l = { ':BufferLineCloseLeft<CR>', 'close the left buff' },
    r = { ':BufferLineCloseRight<CR>', 'close the right buff' },
    n = { ':BufferLineCycleNext<CR>', 'buffer prev' },
    p = { ':BufferLineCyclePrev<CR>', 'buffer prev' },
  },
  f = {
    name = 'search',
    -- 查找文件
    f = { ':Telescope find_files<CR>', 'find file' },
    -- 全局搜索
    g = { ':Telescope live_grep<CR>', 'global search' },
  },
  t = {
    -- 自定义 toggleterm 3个不同类型的命令行窗口
    -- <leader>ta 浮动
    -- <leader>tb 右侧
    -- <leader>tc 下方
    -- 特殊lazygit 窗口，需要安装lazygit
    -- <leader>tg lazygit
    name = 'terminal',
    a = {
      ":lua require('plugin-config/toggleterm').toggleA()<CR>",
      'floatting open',
    },
    b = {
      ":lua require('plugin-config/toggleterm').toggleB()<CR>",
      'open to the right',
    },
    c = {
      ":lua require('plugin-config/toggleterm').toggleC()<CR>",
      'open to the button',
    },
  },
  a = {
    name = 'code',
    a = { '<Plug>(coc-codeaction)', 'code action' },
    d = { '<Plug>(coc-definition)', 'define' },
    i = { '<Plug>(coc-implementation)', 'implement' },
    x = { '<Plug>(coc-references)', 'reference' },
    p = { '<Plug>(coc-diagnostic-prev)', 'diagnostic prev' },
    n = { '<Plug>(coc-diagnostic-next)', 'diagnostic next' },
    f = { '<Plug>(coc-fix-current)', 'fix current' },
    r = { '<Plug>(coc-rename)', 'rename' },
    c = { ':ComAnnotation<Cr>', 'annotation' },
    o = { ':CocOutline<CR>', 'open outline' },
  },
  g = {
    name = 'git',
    -- d = { ':lua package.loaded.gitsigns.diffthis()<CR>', 'diffthis' },
    -- D = { ":lua package.loaded.gitsigns.diffthis('~')<CR>", 'diffthis ~' },
    j = { ":lua package.loaded.gitsigns.next_hunk('')<CR>", 'next hunk' },
    k = { ":lua package.loaded.gitsigns.prev_hunk('')<CR>", 'prev hunk' },
    d = { ':DiffviewFileHistory<CR>', 'diffthis' },
    o = { ':DiffviewOpen<CR>', 'open diff tree' },
    c = { ':DiffviewClose<CR>', 'close diff tree' },
    l = {
      name = 'lazygit',
      l = { ':call LazyGit<CR>', 'open lazygit' },
      c = { ':call LazyGitFilterCurrentFile<CR>', 'open lazygit' },
    },
  },
  --[[ mapbuf('n', 'gd', "<cmd>lua require'telescope.builtin'.lsp_definitions({ initial_mode = 'normal', })<CR>", opt) ]]
}

wk.register(mappingsSpace, optsSpace)

-- local optsAlt = {
--     mode = 'n', -- NORMAL mode
--     prefix = '<Alt>',
--     buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings
--     silent = true, -- use `silent` when creating keymaps
--     noremap = true, -- use `noremap` when creating keymaps
--     nowait = true, -- use `nowait` when creating keymaps
-- }
--
-- local mappintsAlt = { h = { '<C-w>h', 'Split the screen horizontally' } }
--
-- wk.re
